const mvc = require('cosjs.mvc');
//每日全服boss，累积玩家打boss的伤害写入排行榜，然后每天登录领取奖励

/**
 * @name dailyBoss
 * 每日boss
 */

/**
 * @name getRank
 * @param string uid 对手ID
 * 获取排名
 */
exports.getRank = async function () {
    let id = this.get('uid', 'string');
    if (!id) {
        return this.quick_error('N2BS1', "参数缺失");
    }
    let today = mvc.library("time/format", 'yyyyMMdd', this.updater.time);
    let playerRedisSort = this.model.redis("sort", this.sid, "dailyBoss-" + today);
    let rank = await playerRedisSort.get(id);
    let total = await playerRedisSort.size();
    // if (rank <= 100) {
    //     return rank;
    // } else {
    //     let percentile = ((rank - 100) / (total - 100));
    //     return percentile.toFixed(2);
    // }
    return { "rank": rank, "total": total }
}

/**
 * @name getAllRank
 * 获取前100排名
 */
exports.getAllRank = async function () {
    let today = mvc.library("time/format", 'yyyyMMdd', this.updater.time);
    let playerRedisSort = this.model.redis("sort", this.sid, "dailyBoss-" + today);
    // let rank = await playerRedisSort.get(id);
    let ret = await playerRedisSort.range(0, 100, "WITHSCORES");
    return ret;
}


/**
 * @name endBattle
 * @param int hp 血量
 * 结束战斗
 */
exports.endBattle = async function () {
    let hp = this.get('hp', 'int');
    let today = mvc.library("time/format", 'yyyyMMdd', this.updater.time);
    let playerRedisSort = this.model.redis("sort", this.sid, "dailyBoss-" + today);

    // 判断是否要更新伤害，如果hp比排行榜里的分数要高，就更新
    let oldScore = await playerRedisSort.val(this.uid);
    if (oldScore >= hp) {
        return parseInt(oldScore);
    } else {
        // 更新排行榜
        await playerRedisSort.set(this.uid, hp);
    }
    //判断是否有宝箱可以领取
    this.updater.daily.key('dailyBoss');
    await this.updater.data();
    let maxScore = this.updater.daily.get('dailyBoss') || 0;

    //给maxScore到hp中间的奖励
    let bossRewardConfig = this.config("bossReward");
    for (let k in bossRewardConfig) {
        if (k <= hp && k >= maxScore) {
            for (let i = 0; i < bossRewardConfig[k].itemDrop.length; i++) {
                this.updater.item.add(bossRewardConfig[k].itemDrop[i].id, bossRewardConfig[k].itemDrop[i].num);
            }
        }
    }
    if (maxScore <= hp) {
        this.updater.daily.set('dailyBoss', hp);
    }
    await this.updater.save();
    return hp;
}

/**
 * @name getReward
 * 领取积分
 */
exports.getReward = async function () {
    //从排行榜里读取自己的排名，如果前100名的，就从A表读奖励发放
    //100名以后的，计算出自己在整体排名的前百分之几
    //然后从B表读取奖励发放
    this.updater.daily.key('dailyBosddReward');
    await this.updater.data();
    if (this.updater.daily.get('dailyBosddReward')) {
        return this.quick_error('N2BS2', "已领取");
    }
    let today = mvc.library("time/format", 'yyyyMMdd', this.updater.time);
    let playerRedisSort = this.model.redis("sort", this.sid, "dailyBoss-" + today);
    let rank = await playerRedisSort.get(this.uid);
    let total = await playerRedisSort.size();
    await playerRedisSort.set(this.uid, 111);
    if (rank == -1) {
        return this.quick_error('N2BS3', "未找到排名");
    }
    rank += 1;
    if (total <= 0) {
        return this.quick_error('N2BS4', "未找到排名");
    }
    let reward;
    if (rank <= 100) {
        // 前100名，从A表读奖励
        reward = this.config("firstDailyBossRank");
        if (!reward) {
            return this.quick_error('N2BS5', "数据表出错:firstDailyBossRank");
        }
        for (let k in reward) {
            if (rank >= reward[k].rankStart && rank <= reward[k].rankEnd) {
                for (let i = 0; i < reward[k].itemDrop.length; i++) {
                    this.updater.item.add(reward[k].itemDrop[i].id, reward[k].itemDrop[i].num);
                }
            }
        }
    } else {
        // 100名以后，计算排名百分比
        let percentile = Math.ceil((rank / total) * 100);
        // 从B表读取奖励
        reward = this.config("secondDailyBossRank");
        if (!reward) {
            return this.quick_error('N2BS6', "数据表出错:secondDailyBossRank");
        }
        for (let k in reward) {
            if (percentile >= reward[k].rankStart && percentile <= reward[k].rankEnd) {
                for (let i = 0; i < reward[k].itemDrop.length; i++) {
                    this.updater.item.add(reward[k].itemDrop[i].id, reward[k].itemDrop[i].num);
                }
            }
        }
    }
    this.updater.daily.set('dailyBosddReward', 1);
    await this.updater.save();
    return;
}

